<template>
  <div class="login-container">

    <div class="change-language">
     <el-dropdown>
       <el-button type="primary">
        {{ this.$i18n.locale=='CN'?'中文':'English'}}<i class="el-icon-arrow-down el-icon--right"></i>
      </el-button>
      <el-dropdown-menu slot="dropdown">
        <el-dropdown-item @click.native="changeLanguage('CN')">中文</el-dropdown-item>
        <el-dropdown-item @click.native="changeLanguage('EN')">English</el-dropdown-item>
      </el-dropdown-menu>
      </el-dropdown>
    </div>

    <el-form class="login-form" autoComplete="on" :model="loginForm" :rules="loginRules" ref="loginForm"
             label-position="left">
      <div class="title-container">
        <h3 class="title">{{$t('language.test.title')}}</h3>
      </div>
      <el-form-item prop="username">
        <span class="svg-container svg-container_login">
          <svg-icon icon-class="user"/>
        </span>
        <el-input name="username" type="text" v-model="loginForm.username" autoComplete="on" placeholder="username"/>
      </el-form-item>

      <el-form-item prop="password">
        <span class="svg-container">
          <svg-icon icon-class="password"/>
        </span>
        <el-input name="password" :type="passwordType" @keyup.enter.native="handleLogin" v-model="loginForm.password"
                  autoComplete="on" placeholder="password"/>
        <span class="show-pwd" @click="showPwd">
          <svg-icon icon-class="eye"/>
        </span>
      </el-form-item>

      <el-form-item prop="code">
        <el-popover
          trigger="manual"
          placement="right"
          width="215"
          popper-class="login-popover"
          :offset=-60
          v-model="showBindInfo">
          <div class="login-help-tip">请先打开您手机上的google Authenticator，扫描以下二维码</div>
          <qriously ref="qrcode" :value='googleValidate' :size='190'  id="mycanvas"></qriously>
          <div slot="reference">
            <span class="svg-container"><svg-icon icon-class="icon" style=""/></span>
            <el-input name="code" type="text" @keyup.enter.native="handleLogin" v-model="loginForm.code"
                      autoComplete="off" :placeholder="$t('language.login.anthentication')"/>
          </div>
        </el-popover>
      </el-form-item>

      <el-row :gutter="20">
        <el-col :span="24">
          <el-button type="primary" style="width:100%;margin-bottom:30px;" :loading="loading"
                     @click.native.prevent="handleLogin">登录
          </el-button>
        </el-col>
<!--        <el-col :span="4">-->
<!--          <el-button type="success" style="width:100%;margin-bottom:30px;" :loading="loading"-->
<!--                     @click.native.prevent="handleBind">绑定-->
<!--          </el-button>-->
<!--        </el-col>-->

      </el-row>
    </el-form>

  </div>
</template>

<script>
import { isvalidUsername } from '@/utils/validate';
import LangSelect from '@/components/LangSelect';
import SocialSign from './socialsignin';
import Vue from 'vue';

export default {
  components: { LangSelect, SocialSign },
  name: 'login',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!isvalidUsername(value)) {
        callback(new Error('Please enter the correct user name'));
      } else {
        callback();
      }
    };
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('The password can not be less than 6 digits'));
      } else {
        callback();
      }
    };
    const validateCode = (rule, value, callback) => {
      if (value != '' && value.length < 6) {
        callback(new Error('The code must be 6 digits'));
      } else if (!value) {
        callback(new Error('code is required'));
      } else {
        callback();
      }
    };
    return {
      loginForm: {
        username: '',
        password: '',
        code: '',
      },
      googleValidate: '',
      showBindInfo: false,
      loginRules: {
        // username: [{ required: true, trigger: 'blur' }],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }],
        code: [{ required: true, trigger: 'blur', validator: validateCode }],
      },
      passwordType: 'password',
      loading: false,
      showDialog: false,
    };
  },
  methods: {
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = '';
      } else {
        this.passwordType = 'password';
      }
    },
    handleLogin() {
      if (this.loginForm.code !== '') {
        this.$refs.loginForm.validate((valid) => {
          if (valid) {
            this.loading = true;
            this.$store.dispatch(types.LOGIN_WITH_CODE, this.loginForm).then(({ result, data }) => {
              this.loading = false;
              if (result) {
                this.$router.push({ path: '/' });
              } else {
                this.$message({
                  message: data.message,
                  type: 'error',
                });
              }
            });
          } else {
            return false;
          }
        });
      } else {
        this.handleBind();
      }
      // });
    },
    changeLanguage(type) {
      sessionStorage.setItem('lang', type);
      if (type == this.$data.defaultLanguage || type == this.$i18n.locale) {
        return;
      }
      let msg = '';
      if (type == 'CN') {
        msg = '切换成功';
      } else {
        msg = 'switch language success';
      }
      this.$message({
        type: 'success',
        message: msg,
      });
      this.$i18n.locale = type;
    },
    handleBind() {
      const { password, username } = this.loginForm;
      if (password && username) {
        this.loading = true;
        this.$store.dispatch(types.LOGIN_CONFIRM, this.loginForm).then(({ result, data }) => {
          this.loading = false;
          if (result) {
            if (data !== '' && data) {
              this.showBindInfo = true;
              this.googleValidate = data;
            } else {
              this.$message({
                message: '您已绑定验证码，请输入验证码后登陆',
                type: 'error',
              });
            }
          } else {
            this.$message({
              message: data.message,
              type: 'error',
            });
          }
        });
      } else {
        return false;
      }
    },
  },
};
</script>

<style rel="stylesheet/scss" lang="scss">
  $bg: #2d3a4b;
  $light_gray: #eeeeee;

  /* reset element-ui css */
  .login-container {

    .change-language{
      display: flex;
      justify-content:flex-end;
      padding: 20px;
      padding-right: 60px;
      box-sizing: border-box;
    }
    .el-input {
      display: inline-block;
      height: 47px;
      width: 85%;

      input {
        background: transparent;
        border: 0px;
        -webkit-appearance: none;
        border-radius: 0px;
        padding: 12px 5px 12px 15px;
        color: $light_gray;
        height: 47px;

        &:-webkit-autofill {
          -webkit-box-shadow: 0 0 0px 1000px $bg inset !important;
          -webkit-text-fill-color: #ffffff !important;
        }
      }
    }

    .el-form-item {
      border: 1px solid rgba(255, 255, 255, 0.1);
      background: rgba(0, 0, 0, 0.1);
      border-radius: 5px;
      color: #454545;
    }

    .login-popover{
      background: #7cca57;
      border-color: #7cca57;
      .popper__arrow{
        background: #2d3a4a;
        border: 0;
        &:after{
          border: 0;
        }
      }
    }
  }
</style>

<style rel="stylesheet/scss" lang="scss" scoped>
  $bg: #2d3a4b;
  $dark_gray: #889aa4;
  $light_gray: #eeeeee;

  .login-container {
    position: fixed;
    height: 100%;
    width: 100%;
    background-color: $bg;

    .login-form {
      position: absolute;
      left: 0;
      right: 0;
      width: 520px;
      padding: 35px 35px 15px 35px;
      margin: 120px auto;
    }

    .tips {
      font-size: 14px;
      color: #ffffff;
      margin-bottom: 10px;

      span {
        &:first-of-type {
          margin-right: 16px;
        }
      }
    }

    .svg-container {
      padding: 6px 5px 6px 15px;
      color: $dark_gray;
      vertical-align: middle;
      width: 30px;
      display: inline-block;

      &_login {
        font-size: 20px;
      }
    }

    .title-container {
      position: relative;

      .title {
        font-size: 26px;
        font-weight: 400;
        color: $light_gray;
        margin: 0px auto 40px auto;
        text-align: center;
        font-weight: bold;
      }

      .set-language {
        color: #ffffff;
        position: absolute;
        top: 5px;
        right: 0px;
      }
    }

    .show-pwd {
      position: absolute;
      right: 10px;
      top: 7px;
      font-size: 16px;
      color: $dark_gray;
      cursor: pointer;
      user-select: none;
    }

    .thirdparty-button {
      position: absolute;
      right: 35px;
      bottom: 28px;
    }

    .login-QRCode {
      width: 190px;
      height: 190px;

    }

    .login-help-tip{
      word-break: break-all;
      color: #fff;
    }
  }
</style>
